package com.elcl.widget.view.indictor;

import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by lichunlin on 17/6/27.
 * 点击后可以收缩或者展开的layout
 *
 */

public class ExplandanableLayout {

    private int viewMax = 0;//展开的最大的viewheight的max
    private int viewShrink = 0;//收缩后的view的height
    private View view;//view执行的动画
    private boolean isExplandable = false;//是否展开(默认收缩的)

    public ExplandanableLayout(View view, int viewMax, int viewShrink) {
        this.viewMax = viewMax;
        this.viewShrink = viewShrink;
        this.view = view;
    }

    //点击伸缩按钮是，将全部标签变为2行还是全部显示
    public void clickToggleStretch() {
        int heightStart = 0;
        int heightEnd = 0;

        if (isExplandable) {//收缩
            heightStart = viewMax;
            heightEnd = viewShrink;
        } else {//展开
            heightStart = viewShrink;
            heightEnd = viewMax;
        }

        isExplandable = !isExplandable;
        ValueAnimator animator = createDropAnimator(view, heightStart,
                heightEnd);
        animator.start();
    }


    //开始执行value的动画
    private ValueAnimator createDropAnimator(final View v, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator arg0) {
                int value = (int) arg0.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                layoutParams.height = value;
                v.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }


    public boolean isExplandable() {
        return isExplandable;
    }

    public void setExplandable(boolean explandable) {
        isExplandable = explandable;
    }
}
